#include "svgrectangleitem.h"

SvgRectangleItem::SvgRectangleItem(QPointF start, QPointF end, int Width, QColor color, QBrush brush_input):
    CShape(start,end, Width, color, brush_input)
{
}
SvgRectangleItem::SvgRectangleItem(qreal start_x,qreal start_y, qreal end_x, qreal end_y,int Width, QColor color, QBrush brush_input):
    CShape(start_x, start_y, end_x, end_y,Width, color, brush_input)
{
}
void SvgRectangleItem::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget ) {
    Q_UNUSED(widget);
    painter->setPen(this->pen());
    painter->setBrush(this->brush());
    painter->drawRect(boundingRect().adjusted(POINT_SIZE.width()/2,POINT_SIZE.height()/2,-POINT_SIZE.width()/2,-POINT_SIZE.height()/2));
    if(option->state & QStyle::State_Selected) PaintBoundingandControlPoints(painter,boundingRect().adjusted(POINT_SIZE.width()/2,POINT_SIZE.height()/2,-POINT_SIZE.width()/2,-POINT_SIZE.height()/2));
}
// shape、paint与椭圆及其类似
QPainterPath SvgRectangleItem::shape() const{
    QPainterPath path;
    path.moveTo(points[S1]);
    path.lineTo(points[S2]);
    path.lineTo(points[E1]);
    path.lineTo(points[E2]);
    path.closeSubpath();
    QPainterPathStroker stroker;
    stroker.setWidth(pen().widthF()+10);
    QPainterPath strokerPath = stroker.createStroke(path);
    for (int i = 0; i < 8; i++) {
        strokerPath.addRect(PointsforDraw[i]);
    }
    if(brush() != Qt::NoBrush) strokerPath.addRect(path.boundingRect());
    return strokerPath;
}

QRectF SvgRectangleItem::boundingRect() const {
    return shape().boundingRect();
}



void SvgRectangleItem::mousePressEvent(QGraphicsSceneMouseEvent* event) {
    WitchPointsisSelected = -1;
    isRotating = false;
    if (event->buttons() & Qt::LeftButton && event->modifiers() & Qt::ShiftModifier) {
        isRotating = true;
        setSelected(true);
        BeginScenePos = event->scenePos();
        BeginTransform = transform();
    }
    else {
        setCursor(Qt::CursorShape::SizeAllCursor);
        for (int i = 0; i < 8 ; i++) {
            if (PointsforDraw[i].contains(event->pos()))
            {
                WitchPointsisSelected = i;
                setSelected(true);
                break;
            }
        }
        if (WitchPointsisSelected != -1) {
            // 将被选中点的位置更新到鼠标位置实现吸附效果
            MoveControlPoint(event->pos());
            if (WitchPointsisSelected<4&&event->modifiers() & Qt::ControlModifier) {
                int theOne = WitchPointsisSelected;
                int Oppsite = theOne<2?theOne+2:theOne-2;
                QPointF Delta = points[Oppsite] - points[theOne];
                int fx=Delta.x()<0?-1:1,fy=Delta.y()<0?-1:1;
                qreal max = fx*Delta.x()>fy*Delta.y()?fx*Delta.x():fy*Delta.y();
                Delta.rx() = max*fx;
                Delta.ry() = max*fy;
                WitchPointsisSelected = Oppsite;
                MoveControlPoint(event->pos()+Delta);
                WitchPointsisSelected = theOne;
            }
            prepareGeometryChange();
            update();
        }
        else QGraphicsItem::mousePressEvent(event);
    }
}
void SvgRectangleItem::mouseMoveEvent(QGraphicsSceneMouseEvent* event) {
    if (WitchPointsisSelected != -1)
    {
        MoveControlPoint(event->pos());
        setSelected(true);
        prepareGeometryChange();
        update();
    }
    else if (isRotating&&(event->buttons() & Qt::LeftButton && event->modifiers() & Qt::ShiftModifier)) {
        setSelected(true);
        SupportRotate(event->scenePos(),mapToScene(boundingRect().center()));
    }
    else QGraphicsItem::mouseMoveEvent(event);
}
void SvgRectangleItem::mouseReleaseEvent(QGraphicsSceneMouseEvent* event) {
    setCursor(Qt::CursorShape::ArrowCursor);
    if(WitchPointsisSelected != -1) {
        WitchPointsisSelected = -1;
        update();
    }
    QGraphicsItem::mouseReleaseEvent(event);
}
